﻿using System;
using System.IO;
using System.Web;
using Token.Util;
using Token.Piker.Model;
using Token.Piker.BLL;
using Token.Piker.Common;

namespace Token.Piker.Web.AJAX
{
    /// <summary>
    /// Thumbnail 的摘要说明
    /// </summary>
    public class Thumbnail : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.ClearContent();
            context.Response.ContentType = "image/jpeg";
            context.Response.BufferOutput = true;
            int pid = int.Parse(context.Request.QueryString["pid"]);
            int imageWidth = WebHelper.IntSafeConvert(context.Request.QueryString["width"], 0);
            int imageHeight = WebHelper.IntSafeConvert(context.Request.QueryString["height"], 0);

            if (imageWidth < 0) imageWidth = 0;
            if (imageHeight < 0) imageHeight = 0;

            string url = String.Empty;

            try
            {
                //判断是要获取新闻图片还是用户图片
                if (context.Request["newsphoto"] == null)
                {
                    //用户图片
                    Photo photo = (new PhotoBLL()).GetModel(pid);

                    switch (FetchSize(imageWidth, imageHeight))
                    {
                        case 0:
                            url = photo.CompressUrl_1;
                            break;
                        case 1:
                            url = photo.CompressUrl_2;
                            break;
                        case 2:
                            url = photo.CompressUrl_3;
                            break;
                        case 3:
                            url = photo.CompressUrl_4;
                            break;
                    }
                }
                else
                {
                    //新闻图片
                    NewsPhoto photo = (new NewsPhotoBLL()).GetModel(pid);

                    switch (FetchSize(imageWidth, imageHeight))
                    {
                        case 0:
                            url = photo.CompressUrl_1;
                            break;
                        case 1:
                            url = photo.CompressUrl_2;
                            break;
                        case 2:
                            url = photo.CompressUrl_3;
                            break;
                        case 3:
                            url = photo.CompressUrl_4;
                            break;
                    }
                }
                if (!File.Exists(context.Request.MapPath(url)))
                {
                    url = context.Request.MapPath("~/images/notfound.gif");   //使用默认图片
                }

                FileInfo fi = new FileInfo(context.Request.MapPath(url));
                context.Response.TransmitFile(fi.FullName);
                context.Response.Flush();
            }
            catch
            {
                //错误处理
                FileInfo fi = new FileInfo(context.Request.MapPath("~/images/notfound.gif"));
                context.Response.TransmitFile(fi.FullName);
                context.Response.Flush();
            }
            finally
            {
                //////////////////////////////////////////////////////////////////////////
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 获取所对应的图片大小
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        protected int FetchSize(int width, int height)
        {
            int fetch = 3;
            for (int i = 0; i < 4; i++)
            {
                if (width <= GlobalConstants.Photo_Size[1, i] && height <= GlobalConstants.Photo_Size[0, i])
                {
                    fetch = i;
                    break;
                }
            }
            return fetch;
        }
    }
}